package com.level2app.ui.welcome;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.databinding.ActivitySignNameBinding;
import com.level2app.ui.welcome.view.SignNameViewModel;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.KeyboardUtils;
import com.level2app.utils.ToastHelper;

/**
 * 注册--输入名字
 */
public class SignNameActivity extends BaseActivity<ActivitySignNameBinding, SignNameViewModel> {
    private boolean isChooseTop, isChooseBottom;

    @Override
    protected SignNameViewModel bindModel() {
        return getViewModel(this, SignNameViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        binding.termsTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.termsTv.getPaint().setAntiAlias(true);
        binding.privacyPolicyTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.privacyPolicyTv.getPaint().setAntiAlias(true);
        binding.setMViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.termsTv, o -> ToastHelper.showToast("Terms"));
        viewModel.onDelayClick(binding.privacyPolicyTv, o -> ToastHelper.showToast("Privacy Policy"));
        viewModel.firstName.observe(this, s -> changeBtnStatus());
        viewModel.lastName.observe(this, s -> changeBtnStatus());
        viewModel.onDelayClick(binding.topIv, view -> {
            isChooseTop = !isChooseTop;
            binding.topIv.setImageResource(isChooseTop ? R.mipmap.icon_check_sel : R.mipmap.icon_check_def);
            changeBtnStatus();
        });
        viewModel.onDelayClick(binding.bottomIv, view -> {
            isChooseBottom = !isChooseBottom;
            binding.bottomIv.setImageResource(isChooseBottom ? R.mipmap.icon_check_sel : R.mipmap.icon_check_def);
            changeBtnStatus();
        });
        viewModel.onDelayClick(binding.nextTv, o -> {
            if (!isChooseTop) {
                ToastHelper.showToastAndImage(getString(R.string.input_name_age_limit), R.mipmap.icon_toast_error);
                return;
            }
            if (!isChooseBottom) {
                ToastHelper.showToastAndImage(getString(R.string.input_name_privacy_policy_limit), R.mipmap.icon_toast_error);
                return;
            }
            JumpIntent.jumpToSignFinish(this);
        });
    }

    private void changeBtnStatus() {
        if (viewModel.firstName.getValue() != null && viewModel.firstName.getValue().trim().length() > 0
                && viewModel.lastName.getValue() != null && viewModel.lastName.getValue().trim().length() > 0
                && isChooseTop && isChooseBottom) {
            binding.nextTv.setBackgroundResource(R.drawable.shape_btn_bg);
            binding.nextTv.setTextColor(Color.WHITE);
            binding.nextTv.setEnabled(true);
        } else {
            binding.nextTv.setBackgroundResource(R.drawable.shape_btn_bg_def);
            binding.nextTv.setTextColor(getColor(R.color.color_9ca3af));
            binding.nextTv.setEnabled(false);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}